//
//  F_AllScence_ViewController.swift
//  MyFood
//
//  Created by dllo on 16/2/21.
//  Copyright © 2016年 XRG. All rights reserved.
//

import UIKit
import SwiftyJSON

class F_AllScence_ViewController: BaseViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    var collectionView : UICollectionView!
    var scenesArr = Array<F_Scence_Model>()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "全部场景"
        createCollection()
        createData()
        // Do any additional setup after loading the view.
    }

    func createCollection(){

        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSizeMake((BASE_WIDTH - 5)/3, (BASE_WIDTH - 5)/2)
        self.collectionView = UICollectionView(frame: CGRectMake(0, 0, BASE_WIDTH, BASE_HEIGHT), collectionViewLayout: flowLayout)
        self.view.addSubview(self.collectionView)
        flowLayout.scrollDirection = UICollectionViewScrollDirection.Vertical
        flowLayout.minimumInteritemSpacing = 2.5
        flowLayout.minimumLineSpacing = 2.5
        collectionView.pagingEnabled = true
        collectionView.bounces = false
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = UIColor.whiteColor()
        self.collectionView.registerNib(UINib(nibName: "R_AllScence_CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell")
    }

    func createData(){

        XRAFNetworkTool.POSTUrl(BASE_URL, body: "page=1&methodName=SceneList&size=20&version=4.3.1&", requestBodyStyle: XRRequestStyle.String, headFile: nil, response: XRResponseStyle.XRJSON, success: { (task, result) -> Void in

            let json = JSON(result)
            let arr = json["data"]["data"].array
            for temp in arr!{
            let dic : Dictionary = temp.dictionaryObject!
            let sceneModel = F_Scence_Model(dic:dic)
            self.scenesArr.append(sceneModel)
            }

            if self.scenesArr.count != 0{
                self.collectionView.reloadData()
            }
            }) { (task, error) -> Void in
                print(error)
        }
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.scenesArr.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell : R_AllScence_CollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! R_AllScence_CollectionViewCell
        let sceneModel = self.scenesArr[indexPath.row]
        cell.titleLabel.text = sceneModel.scene_title
        cell.img.sd_setImageWithURL(NSURL(string: sceneModel.scene_background), placeholderImage: UIImage(named: "空白1"))
        cell.descLabel.text = "\(sceneModel.dish_count)道菜"
        return cell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {


        let model = self.scenesArr[indexPath.row]
//        let sigleSceneVC = F_SingleScence_ViewController()
//        sigleSceneVC.sceneID = model.scene_id
//        // 自己铺也有数据
// self.navigationController?.pushViewController(sigleSceneVC, animated: true)

        // 转到Web
        let url =  "http://h5.izhangchu.com/web/scene/view.html?&scene_id=\(model.scene_id)"
        print(model.scene_id)
        let webVC = F_WebView_ViewController()
        self.navigationController?.pushViewController(webVC, animated: true)
        webVC.webUrl = url


    }


    


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
